package com.lantosec.hmrp.modules.form.controller;

import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.form.entity.FormLayout;
import com.lantosec.hmrp.modules.form.service.IFormLayoutService;
import com.lantosec.hmrp.modules.form.vo.FormLayoutVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * 自定义表单布局 控制器
 *
 * @author AutoCode
 * @since 2020-04-15
 */
@Api(value = "自定义表单布局控制器", tags = "自定义表单布局控制器")
@RestController
@RequestMapping("/form/layout/")
@Log4j2
public class FormLayoutController extends BaseController {
    @Autowired
    private IFormLayoutService service;

    @GetMapping("query/{tableId}")
    @ApiOperation(value = "根据表格Id查询")
    public Result<List<FormLayoutVO>> query(@ApiParam(value = "表格Id", required = true) @PathVariable(name="tableId") String tableId) {
        return Result.ok(service.queryByTableId(tableId));
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<FormLayoutVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("create")
    @ControllerLog(name = "自定义表单布局新增数据", type = LogType.add)
    @ApiOperation(value = "新增数据")
    public Result<String> create(@ApiParam(value = "自定义表单布局VO对象", required = true) @RequestBody @Validated FormLayoutVO vo){
        FormLayout entity = vo.convertToEntity();
        entity.setCreateUserId(super.getUserId());
        entity.setCreateUserName(super.getName());
        return service.saveLayout(entity);
    }

    @PutMapping("update")
    @ControllerLog(name = "自定义表单布局修改数据", type = LogType.update)
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "自定义表单布局VO对象", required = true) @RequestBody FormLayoutVO vo){
        FormLayout entity = vo.convertToEntity();
        entity.setCreateUserId(super.getUserId());
        entity.setCreateUserName(super.getName());
        return service.updateLayout(entity);
    }

    @DeleteMapping(value = "delete/{id}")
    @ControllerLog(name = "自定义表单布局删除数据", type = LogType.delete)
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        if(StringUtils.isNotEmpty(id)){
            service.removeById(id);
        }
        return Result.ok();
    }
}
